<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <%@include file="../../common.jsp"%>
    <base href="<%=basePath%>">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <title>系统配置</title>
</head>
<body>
    <h2>系统配置</h2>
    <hr>

    <table class="layui-table" id="configTable" lay-filter="configTable"></table>

    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-circle"></i>添加</button>
            <button class="layui-btn layui-btn-sm" lay-event="export"><i class="layui-icon layui-icon-export"></i> 导出数据</button>
        </div>
    </script>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" href="javascript:void(0)">修改</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" href="javascript:void(0)">删除</a>
    </script>

    <script>
        layui.use(['form','table','form'],function () {
            var table = layui.table;
            //表格渲染
            $.get("/systemConfig/selectInfo",function (data) {
                table.render({
                    id:'configTable',
                    elem:'#configTable',
                    url:'systemConfig/selectInfo',
                    page:'true',
                    even:'false',
                    toolbar:'#toolbarDemo',
                    cols:[[
                        {field:'id', title:'编号'},
                        {field:'configCode', title:'配置类型编码'},
                        {field:'configName', title:'配置类型名称'},
                        {field:'configValueId', title:'类型编号'},
                        {field:'configValueName', title:'类型名称'},
                        {field:'isStart',title:'启用状态',templet:function (d) {
                                var str = d.isStart == 1 ? "checked" : "";
                                var check = '<input type="checkbox" title="启用" '+str+'>';
                                return check;
                            }},
                        {toolbar:'#barDemo',title:'操作'}
                    ]]
                })
            })
            //监听头部工具栏
            table.on("toolbar(configTable)",function (obj) {
                var choose = obj.event;
                switch (choose) {
                    case 'add':
                        $.get("/systemConfig/form",function (str) {
                            layer.open({
                                type:1,
                                title:'添加配置',
                                content:str,
                                area:'700px',
                                skin:'layui-layer-molv',
                                btn:['确定','取消'],
                                success:function () {
                                    layui.form.render();
                                },
                                yes:function (index) {
                                    var params = $("#configForm").serialize();
                                    console.log(JSON.stringify(params));
                                    $.post('systemConfig/add',params,function (data) {
                                        if(data){
                                            layer.close(index);
                                            layer.msg("添加成功",{icon:1});
                                            table.reload("configTable");
                                        }else{
                                            layer.msg("添加失败",{icon:2});
                                        }
                                    })
                                }
                            })
                        })
                        break;
                }
            })

            //监听行内事件
            table.on("tool(configTable)",function (obj) {
                var data = obj.data;
                if(obj.event == 'del'){
                    layer.open({
                        time:0,
                        title:'系统提示',
                        content:'确定要删除吗?',
                        btn:['确定','取消'],
                        yes:function () {
                            console.log(obj.data.id);
                            $.post("/systemConfig/del",{"configId":obj.data.id},function (data) {
                                if(data){
                                    layer.msg('删除成功！！！');
                                    table.reload('configTable');
                                }
                            })
                        }
                    })
                }else if(obj.event == 'edit'){
                    $.get("/systemConfig/form",function (str) {
                        layer.open({
                            type:1,
                            title:'修改配置',
                            content:str,
                            area:'700px',
                            skin:'layui-layer-molv',
                            btn:['确定','取消'],
                            success:function () {
                                $.get("systemConfig/get",{"id":obj.data.id},function (data) {
                                    layui.form.val('configForm',data)
                                })
                                layui.form.render();
                            },
                            yes:function (index) {
                                var params = $("#configForm").serialize();
                                console.log(JSON.stringify(params));
                                $.post('systemConfig/update',params,function (data) {
                                    if(data){
                                        layer.close(index);
                                        layer.msg("修改成功",{icon:1});
                                        table.reload("configTable");
                                    }else{
                                        layer.msg("修改失败!",{icon:2});
                                    }
                                })
                            }
                        })
                    })
                }
            })
        })
    </script>
</body>
</html>